from selenium import webdriver
import os
import time
from Logs.mylog import log1
import getcwd
import configparser

path = getcwd.get_cwd()
config_path = os.path.join(path, 'Config/config.ini')
# 封装webdriver定位元素，操作ui界面的一些常用方法，方便方法的复用


class BasePage(object):

    def __init__(self, driver):
        self.driver = driver

    def geturl(self, url):
        return self.driver.get(url)
# dwff：定位方法
# yuansu：元素
    dwffys = ['type', 'xpath']

    def findelements(self, dwffys):
        if dwffys[0] == 'id':
            return self.driver.find_element_by_id(dwffys[1])
        elif dwffys[0] == 'class':
            return self.driver.find_element_by_class_name(dwffys[1])
        elif dwffys[0] == 'xpath':
            return self.driver.find_elements_by_xpath(dwffys[1])
        elif dwffys[0] == 'link_text':
            return self.driver.find_element_by_link_text(dwffys[1])
        elif dwffys[0] == 'tag_name':
            return self.driver.find_element_by_tag_name(dwffys[1])
        else:
            print('没有找到相应的元素定位方法')

    def onclick(self, dwffys):
        self.findelements(dwffys).click()

    def send_keys(self, dwffys, value):
        self.findelements(dwffys).send_keys(value)

    def get_img(self):
        '''截图'''
        path = os.path.join(os.path.dirname(os.getcwd()), 'screenshots/')  # 拼接截图保存路径
        rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))  # 按格式获取当前时间
        screen_name = path + rq + '.png'  # 拼接截图文件名
        try:
            self.driver.get_screenshot_as_file(screen_name)
            log1.info("截图保存成功")
        except BaseException:
            log1.error("截图失败", exc_info=1)

    #  配置文件增删改查
    def config_get(self, key, section=None):
        ''' 读取配置文件字段的值并返回 '''
        config = configparser.ConfigParser()
        config.read(config_path, encoding="utf-8-sig")
        switch = config.get('environment', 'switch')
        if section==None and switch == str(0):
            section = 'test'
        elif section==None and switch == str(1):
            section = 'prod'
        config_get = config.get(section, key)
        return  config_get

    #  写入配置文件
    def config_write(self, key=None, value=None, section=None):
        '''往配置文件写入键值'''
        config = configparser.ConfigParser()
        config.read(config_path, encoding="utf-8-sig")
        switch = config.get('environment', 'switch')
        if section == None and switch == str(0):
            section = 'test'
        elif section == None and switch == str(1):
            section = 'prod'
        if key is not None and value is not None:
            config.set(section,key,value)
            log1.info('在section:%s下写入%s=%s' %(section,key,value))
            with open(config_path,'w',encoding='utf-8')as f :
                config.write(f)
        else:
            config.add_section(section)
            log1.info('新增section：%s' % section)
            with open(config_path,'w',encoding='utf-8')as f :
                config.write(f)

    #删除配置文件
    def config_delete(self, key=None, section=None):
        '''删除配置文件字段'''
        config = configparser.ConfigParser()
        config.read(config_path, encoding="utf-8-sig")
        switch = config.get('environment', 'switch')
        if section == None and switch == str(0):
            section = 'test'
        elif section == None and switch == str(1):
            section = 'prod'
        if key is not None:
            config.remove_option(section, key)
            log1.info('删除section:%s下key为：%s的记录' % (section, key))
            with open(config_path, 'w', encoding='utf-8')as f:
                config.write(f)
        else:
            config.remove_section(section)
            log1.info('删除section:%s' % section)
            with open(config_path, 'w', encoding='utf-8')as f:
                config.write(f)

    #  根据配置文件，对浏览器进行封装
    def open_browser(self):
        browser = self.config_get(key='browser', section='environment')
        log1.info('读取浏览器配置')
        url = self.config_get('url')
        log1.info('读取url：%s' % url)
        try:
            if browser == str(0):
                driver = webdriver.Chrome()
                log1.info('打开的浏览器为chrome')
            elif browser == str(1):
                driver = webdriver.Firefox()
                log1.info('打开的浏览器为chrome')
            driver.get(url)
            driver.maximize_window()
            log1.info('浏览器最大化')
            driver.implicitly_wait(10)
            log1.info('设置静态等待时间10秒')
            return driver
        except BaseException:
            log1.error('浏览器打开报错')

    def quit(self):
        self.driver.quit()



















